package com.h3c.web.support.console.impl;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.tuple.Pair;

import com.h3c.dal.entity.query.CasssoOrganizationUnitQuery;
import com.h3c.dal.entity.query.CasssoUserQuery;
import com.h3c.web.common.constant.SessionConstant;
import com.h3c.web.common.constant.RoleConstant.RoleEnum;
import com.h3c.web.support.console.OrgSupport;
import com.h3c.web.support.console.RoleSupport;

public class OrgSupportImpl implements OrgSupport {
	private RoleSupport roleSupport;

	@Override
	public Pair<CasssoUserQuery, CasssoUserQuery.Criteria> generateUserQuery(HttpServletRequest request)
			throws Exception {
		RoleEnum role = roleSupport.getRole(request);
		CasssoUserQuery example = new CasssoUserQuery();
		CasssoUserQuery.Criteria criteria = example.createCriteria();
		if (RoleEnum.org_admin.equals(role)) {
			String ouId = (String) request.getSession().getAttribute(SessionConstant.OU_ID);
			criteria.andOuIdEqualTo(ouId);
		}
		return Pair.of(example, criteria);
	}
	@Override
	public Pair<CasssoOrganizationUnitQuery, CasssoOrganizationUnitQuery.Criteria> generateOrgQuery(
			HttpServletRequest request) throws Exception {
		RoleEnum role = roleSupport.getRole(request);
		CasssoOrganizationUnitQuery example = new CasssoOrganizationUnitQuery();
		CasssoOrganizationUnitQuery.Criteria criteria = example.createCriteria();
		if (RoleEnum.org_admin.equals(role)) {
			String ouId = (String) request.getSession().getAttribute(SessionConstant.OU_ID);
			criteria.andOuIdEqualTo(ouId);
		}
		return Pair.of(example, criteria);
	}
	public RoleSupport getRoleSupport() {
		return roleSupport;
	}
	public void setRoleSupport(RoleSupport roleSupport) {
		this.roleSupport = roleSupport;
	}
}
